#!/bin/sh
#error and exit = set -e
#set -o errexit
#color
source /etc/init.d/functions
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

function deal(){
  rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org > /dev/null 2>&1

  # Get the system name from /etc/os-release file
  SYSTEM=$(grep -oP '(?<=^NAME=").*(?=")' /etc/os-release)
  # Check the system name and print corresponding information
  case $SYSTEM in
    CentOS\ Linux)
      yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm -y > /dev/null 2>&1
      ;;
    Rocky\ Linux)
      dnf -y install elrepo-release > /dev/null 2>&1
      ;;
    *)
      # If the system is neither CentOS nor Rocky, exit with an error message
      echo "Unsupported system: $SYSTEM"
      exit 1
  esac

  sed -i 's@http://elrepo.org/linux@https://mirrors.aliyun.com/elrepo@g' /etc/yum.repos.d/elrepo.repo
  sed -i 's@mirrorlist=@# mirrorlist=@g' /etc/yum.repos.d/elrepo.repo
  yum clean all > /dev/null 2>&1 && yum makecache > /dev/null 2>&1
  yum --enablerepo=elrepo-kernel install kernel-lt-devel kernel-lt -y > /dev/null 2>&1

  awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg > /tmp/kernel.log 2>&1
  grub2-set-default 0 > /dev/null 2>&1
  grub2-mkconfig -o /etc/grub2.cfg > /dev/null 2>&1
}


function main(){
  action "$0" deal
}

main $*
